/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdesk;

import java.io.IOException;
import java.util.ArrayList;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.RequestDispatcher;

import java.sql.SQLException;

/**
 * This servlet is for creating tickets.
 *
 * The servlet accepts the following parameters
 * 1) expertise - id of the expertise this ticket belongs to. It is the id from
 *    the expertise table in the database.
 * 2) priority - How import is this ticket? Should be from 1 to 5.
 * 3) title - Title of the help ticket, such as 'My Mac won't turn on'
 * 4) description - detailed description of the problem
 *
 * The servlet creates the following attributes for the JSP page:
 * 1) error - any error messages
 * 2) expertises - All valid expertises a ticket can have.
 *
 * The servlet is dependent on the following session variables:
 * 1) user
 *
 * @author brian
 */
public class CreateTicketServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            User user;
            boolean redirect = false;
            String error = "";
            ArrayList<Expertise> expertises = null;

            if ((user = HelpdeskUtil.authenticateUser(request, response)) != null)
            {
                try
                {
                    expertises = Expertise.getExpertises();
                    request.setAttribute("expertises", expertises);

                    if (request.getMethod().equals("POST"))
                    {
                        String expertise = request.getParameter("expertise");
                        int expertiseId = -1;
                        String priorityStr = request.getParameter("priority");
                        int priority = -1;
                        String title = request.getParameter("title");
                        String description = request.getParameter("description");

                        if (expertise == null || priorityStr == null || title == null || description==null)
                        {
                            error = "Fields are missing";
                        }
                        else
                        {
                            try
                            {
                                expertiseId = Integer.parseInt(expertise);
                                priority = Integer.parseInt(priorityStr);
                                if (title.length() < 1 || description.length() < 1)
                                {
                                    error = "Fields are missing";
                                }
                                else
                                {
                                    HelpTicket ticket = new HelpTicket();
                                    ticket.setUserId(user.getId());
                                    ticket.setTitle(title);
                                    ticket.setPriority(priority);
                                    ticket.setExpertiseId(expertiseId);
                                    ticket.setDescription(description);
                                    ticket.create();

                                    response.sendRedirect("viewtickets.jsp");
                                    redirect = true;

                                    HelpdeskUtil.sendEmail(user.getEmail(), "Ticket submitted", "Your ticket, " + ticket.getTitle() + ", has been submitted.");
                                }
                            }
                            catch (NumberFormatException ex)
                            {
                                error = "Fields are missing";
                            }
                        }
                    }
                }
                catch (SQLException ex)
                {
                    error = "SQL error: " + ex.getMessage();
                }

                if (!redirect)
                {
                    request.setAttribute("error", error);
                    RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/createticket.jsp");
                    rd.forward(request, response);
                }
            }

        } finally { 
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
